package com.ruoyi.serviceItem.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.serviceItem.domain.ServiceItem;
import com.ruoyi.serviceItem.service.IServiceItemService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 服务项目Controller
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/item")
public class ServiceItemController extends BaseController
{
    @Autowired
    private IServiceItemService serviceItemService;

    /**
     * 查询服务项目列表
     */
    @RequiresPermissions("serviceItem:item:list")
    @GetMapping("/list")
    public TableDataInfo list(ServiceItem serviceItem)
    {
        startPage();
        List<ServiceItem> list = serviceItemService.selectServiceItemList(serviceItem);
        return getDataTable(list);
    }

    /**
     * 导出服务项目列表
     */
    @RequiresPermissions("serviceItem:item:export")
    @Log(title = "服务项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServiceItem serviceItem)
    {
        List<ServiceItem> list = serviceItemService.selectServiceItemList(serviceItem);
        ExcelUtil<ServiceItem> util = new ExcelUtil<ServiceItem>(ServiceItem.class);
        util.exportExcel(response, list, "服务项目数据");
    }

    /**
     * 获取服务项目详细信息
     */
    @RequiresPermissions("serviceItem:item:query")
    @GetMapping(value = "/{serviceId}")
    public AjaxResult getInfo(@PathVariable("serviceId") Long serviceId)
    {
        return success(serviceItemService.selectServiceItemByServiceId(serviceId));
    }

    /**
     * 新增服务项目
     */
    @RequiresPermissions("serviceItem:item:add")
    @Log(title = "服务项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServiceItem serviceItem)
    {
        return toAjax(serviceItemService.insertServiceItem(serviceItem));
    }

    /**
     * 修改服务项目
     */
    @RequiresPermissions("serviceItem:item:edit")
    @Log(title = "服务项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServiceItem serviceItem)
    {
        return toAjax(serviceItemService.updateServiceItem(serviceItem));
    }

    /**
     * 删除服务项目
     */
    @RequiresPermissions("serviceItem:item:remove")
    @Log(title = "服务项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{serviceIds}")
    public AjaxResult remove(@PathVariable Long[] serviceIds)
    {
        return toAjax(serviceItemService.deleteServiceItemByServiceIds(serviceIds));
    }

    @PutMapping("/status")
    public AjaxResult toggleStatus(@RequestParam Long serviceId,
                                   @RequestParam String status) {
        try {
            serviceItemService.toggleServiceStatus(serviceId, status);
            return AjaxResult.success("状态更新成功");
        } catch (ServiceException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/checkInUse/{serviceId}")
    public AjaxResult checkInUse(@PathVariable Long serviceId) {
        boolean inUse = serviceItemService.checkServiceItemInUse(serviceId);
        return AjaxResult.success(inUse);
    }
}
